package com.example.forum.mapper;

import com.example.forum.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * @author lyk19
 * @description 针对表【t_user】的数据库操作Mapper
 * @createDate 2024-08-01 12:54:53
 * @Entity generator.domain.TUser
 */

@Mapper
public interface UserMapper {

    /**
     * 根据用户名查询用户
     *
     * @param username
     * @return
     */
    @Select("select count(*) from t_user where username = #{username}")
    Integer getByUsername(String username);

    /**
     * 注册用户
     *
     * @param user
     * @return
     */
    @Insert("insert into t_user(username, password, salt, nickname, email, phonenum, gender, avatarurl, isadmin, state, deletestate, createtime, updatetime) " +
            "values(#{username}, #{password}, #{salt}, #{nickname}, #{email}, #{phoneNum}, #{gender}, #{avatarUrl}, #{isAdmin}, #{state}, #{deleteState}, #{createTime}, #{updateTime})")
    Integer insert(User user);

    /**
     * 根据用户名查询用户
     *
     * @param username
     * @return
     */
    @Select("select * from t_user where username = #{username}")
    User getByUser(String username);

    /**
     * 根据用户id查询用户
     *
     * @param id
     * @return
     */
    @Select("select * from t_user where id = #{id}")
    User getUser(Long id);

    /**
     * 更新用户文章数
     *
     * @param userId
     * @return
     */
    @Update("update t_user set articlecount = articlecount + 1 where id = #{userId}")
    Integer updateArticleCount(Long userId);

    /**
     * 删除用户文章数
     *
     * @param id
     * @return
     */
    @Update("update t_user set articlecount = articlecount - 1 where id = #{id}")
    Integer deleteArticleCount(Long id);

    /**
     * 更新用户信息
     *
     * @param newUser
     * @return
     */
    Integer updateByPrimaryKey(User newUser);
}




